package cn.edu.baiyunu.myapplication.ui.create.components

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AddAPhoto
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.CloudUpload
import androidx.compose.material.icons.filled.CollectionsBookmark
import androidx.compose.material.icons.filled.FileDownload
import androidx.compose.material.icons.filled.Image
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.foundation.clickable

/**
 * 照片选择界面
 */
@Composable
fun PhotoSelectionContent(
    isDragging: Boolean,
    onDragStateChanged: (Boolean) -> Unit,
    onSelectImage: () -> Unit,
    onCameraClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    Box(
        modifier = modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp)
                .verticalScroll(rememberScrollState()),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            // 标题
            Text(
                text = "创作新作品",
                fontWeight = FontWeight.Bold,
                fontSize = 24.sp,
                color = Color(0xFF1E293B),
                modifier = Modifier.padding(top = 16.dp, bottom = 8.dp)
            )
            
            // 副标题
            Text(
                text = "上传清晰照片，让AI生成创意作品",
                fontSize = 16.sp,
                color = Color(0xFF64748B),
                modifier = Modifier.padding(bottom = 24.dp)
            )
            
            // 拖放区域
            DragDropArea(
                isDragging = isDragging,
                onDragStateChanged = onDragStateChanged,
                onSelectImage = onSelectImage
            )
            
            Spacer(modifier = Modifier.height(24.dp))
            
            // 照相按钮
            Button(
                onClick = onCameraClick,
                modifier = Modifier.fillMaxWidth(0.7f),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color(0xFF4F46E5),
                    contentColor = Color.White
                ),
                shape = RoundedCornerShape(8.dp)
            ) {
                Icon(
                    imageVector = Icons.Default.AddAPhoto,
                    contentDescription = "拍照",
                    modifier = Modifier.size(20.dp)
                )
                Spacer(modifier = Modifier.width(8.dp))
                Text(text = "使用相机拍摄", fontSize = 16.sp)
            }
            
            Spacer(modifier = Modifier.height(32.dp))
            
            // 照片要求
            PhotoRequirements()
            
            Spacer(modifier = Modifier.height(16.dp))
            
            // 本地下载说明
            LocalDownloadInfo()
        }
    }
}

/**
 * 拖放区域
 */
@Composable
fun DragDropArea(
    isDragging: Boolean,
    onDragStateChanged: (Boolean) -> Unit,
    onSelectImage: () -> Unit,
    modifier: Modifier = Modifier
) {
    val borderColor = if (isDragging) Color(0xFF4F46E5) else Color(0xFFE2E8F0)
    val backgroundColor = if (isDragging) Color(0xFFEEF2FF) else Color(0xFFF8FAFC)
    
    Card(
        modifier = modifier
            .fillMaxWidth()
            .height(200.dp),
        shape = RoundedCornerShape(16.dp),
        colors = CardDefaults.cardColors(
            containerColor = backgroundColor
        ),
        elevation = CardDefaults.cardElevation(
            defaultElevation = 0.dp
        )
    ) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .border(
                    width = 2.dp,
                    color = borderColor,
                    shape = RoundedCornerShape(16.dp)
                )
                .clip(RoundedCornerShape(16.dp))
                .padding(8.dp)
                .clickable { onSelectImage() },
            contentAlignment = Alignment.Center
        ) {
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                verticalArrangement = Arrangement.Center,
                modifier = Modifier.padding(16.dp)
            ) {
                Icon(
                    imageVector = Icons.Default.CloudUpload,
                    contentDescription = "上传图片",
                    tint = Color(0xFF4F46E5),
                    modifier = Modifier.size(48.dp)
                )
                
                Spacer(modifier = Modifier.height(12.dp))
                
                Text(
                    text = "拖放文件到这里或",
                    fontSize = 16.sp,
                    color = Color(0xFF64748B)
                )
                
                Spacer(modifier = Modifier.height(8.dp))
                
                Button(
                    onClick = onSelectImage,
                    colors = ButtonDefaults.buttonColors(
                        containerColor = Color(0xFF4F46E5),
                        contentColor = Color.White
                    ),
                    shape = RoundedCornerShape(8.dp)
                ) {
                    Icon(
                        imageVector = Icons.Default.Image,
                        contentDescription = "选择图片",
                        modifier = Modifier.size(20.dp)
                    )
                    Spacer(modifier = Modifier.width(8.dp))
                    Text(text = "选择照片", fontSize = 16.sp)
                }
            }
        }
    }
}

/**
 * 照片要求
 */
@Composable
fun PhotoRequirements() {
    Card(
        modifier = Modifier.fillMaxWidth(),
        shape = RoundedCornerShape(16.dp),
        colors = CardDefaults.cardColors(
            containerColor = Color.White
        ),
        elevation = CardDefaults.cardElevation(
            defaultElevation = 2.dp
        )
    ) {
        Column(
            modifier = Modifier.padding(16.dp)
        ) {
            Text(
                text = "照片要求",
                fontWeight = FontWeight.Bold,
                fontSize = 18.sp,
                color = Color(0xFF1E293B),
                modifier = Modifier.padding(bottom = 12.dp)
            )
            
            PhotoRequirementItem("照片清晰，人物面部完整")
            PhotoRequirementItem("光线均匀，背景简单")
            PhotoRequirementItem("正面拍摄，保持自然表情")
            PhotoRequirementItem("支持JPG、PNG格式，大小不超过5MB")
        }
    }
}

/**
 * 照片要求项
 */
@Composable
fun PhotoRequirementItem(text: String) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier.padding(vertical = 4.dp)
    ) {
        Icon(
            imageVector = Icons.Default.Check,
            contentDescription = null,
            tint = Color(0xFF4F46E5),
            modifier = Modifier.size(16.dp)
        )
        
        Spacer(modifier = Modifier.width(8.dp))
        
        Text(
            text = text,
            fontSize = 14.sp,
            color = Color(0xFF64748B)
        )
    }
}

/**
 * 本地下载信息
 */
@Composable
fun LocalDownloadInfo() {
    Card(
        modifier = Modifier.fillMaxWidth(),
        shape = RoundedCornerShape(16.dp),
        colors = CardDefaults.cardColors(
            containerColor = Color(0xFFEEF2FF)
        ),
        elevation = CardDefaults.cardElevation(
            defaultElevation = 0.dp
        )
    ) {
        Row(
            modifier = Modifier.padding(16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Icon(
                imageVector = Icons.Default.FileDownload,
                contentDescription = null,
                tint = Color(0xFF4F46E5),
                modifier = Modifier.size(24.dp)
            )
            
            Spacer(modifier = Modifier.width(12.dp))
            
            Text(
                text = "处理完成后，作品将自动保存到本地，您可以在'我的作品'中查看",
                fontSize = 14.sp,
                color = Color(0xFF4F46E5)
            )
        }
    }
} 